package com.wenwei.paper.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.orhanobut.logger.Logger;
import com.wenwei.paper.R;
import com.wenwei.paper.bean.Test;
import com.wenwei.paper.net.ApiDeviceManager;
import com.wenwei.paper.net.ApiManager;
import com.wenwei.paper.net.BaseSubscriber;

import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Response;
import okhttp3.ResponseBody;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

/**
 * Created by Administrator on 2017/7/13.
 */
public class AddPopupWindow extends PopupWindow{

    Activity mActivity;
    PopupWindow popupWindow;
    private int deviceListId;
    public CallBack mCallBack;
    public interface CallBack{
        void onCommitOk();
    }

    TextView okTv;
    TextView cancelTv;
    TextView addTv;
    TextView removeTv;
    TextView numTv;
    public AddPopupWindow(Activity activity, int device, final int num, View parentView, CallBack callBack) {
        this.mCallBack = callBack;
        this.deviceListId = device;
        mActivity = activity;
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.custom_add, null);
        numTv = (TextView) contentView.findViewById(R.id.goods_num);
        numTv.setText(num+"");

        addTv = (TextView) contentView.findViewById(R.id.add_operate);
        addTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Integer.parseInt(numTv.getText().toString())<num){
                    int count = Integer.parseInt(numTv.getText().toString())+1;
                    numTv.setText(count+"");
                }
            }
        });

        removeTv = (TextView) contentView.findViewById(R.id.remove_operate);
        removeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Integer.parseInt(numTv.getText().toString())>0){
                    int count = Integer.parseInt(numTv.getText().toString())-1;
                    numTv.setText(count+"");
                }
            }
        });



        okTv = (TextView) contentView.findViewById(R.id.authentication_ok);
        okTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Integer.parseInt(numTv.getText().toString()) == 0){
                    Toast.makeText(mActivity,"当前补货数量为空",Toast.LENGTH_SHORT).show();
                }else {
                    addGoods();
                }
            }
        });
        cancelTv = (TextView) contentView.findViewById(R.id.authentication_cancel);
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.popwindow_anim_style); // 设置动画
        ColorDrawable cd = new ColorDrawable(0x000000);
        popupWindow.setBackgroundDrawable(cd);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.4f;

        activity.getWindow().setAttributes(lp);
        popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);
            }
        });
    }
    /**
     * 改变背景颜色
     */
    private void darkenBackground(Float bgcolor) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mActivity.getWindow().setAttributes(lp);
    }


    private BaseSubscriber<Test> addSubscriber;
    private void addGoods(){
        addSubscriber = new BaseSubscriber<Test>(mActivity) {
            @Override
            public void onSuccess(Test arrayList) {
                Logger.d(arrayList.toString());
                popupWindow.dismiss();
                mCallBack.onCommitOk();
            }
        };
        try{
            JSONObject jb = new JSONObject();
            jb.put("devicelistId",deviceListId);
            jb.put("num",Integer.parseInt(numTv.getText().toString()));
            ApiDeviceManager.getInstance().addGoods(addSubscriber,deviceListId,Integer.parseInt(numTv.getText().toString()));
        }catch (Exception e){}
    }
}
